package mobao.kafka;

import mobao.module.log.entity.SysLogStackEntity;
import mobao.module.log.service.SysLogStackService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * Created by yanghongquan on 2018/9/13 16:52
 */
@Component
public class LogCustomer {

    @Autowired
    private SysLogStackService sysLogStackService;

    /**
     * Log的Listen
     * @param record
     */
    @KafkaListener(topics = {"moboLog"})
    public void loglisten(ConsumerRecord<?, ?> record){
        Optional<?> kafkaMessage = Optional.ofNullable(record.value());
        if (kafkaMessage.isPresent()) {
            Object message = kafkaMessage.get();
            SysLogStackEntity entity = new SysLogStackEntity();
            entity.setCreateTime(new Date());
            entity.setStackData(message.toString());
            sysLogStackService.insert(entity);
        }

    }

}
